package com.tanhua.dubbo.api;


import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score")))
                .limit(1);
        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    @Override
    public PageResult queryRecommendUser(Integer page, Integer pageSize, Long toUserId) {
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        Query query = Query.query(criteria);
        long count = mongoTemplate.count(query, RecommendUser.class);
//        query.with(Sort.by(Sort.Order.desc("score"))).limit((page - 1) * pageSize).skip(pageSize);
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);
        return new PageResult(page, pageSize, count, list);
    }
}
